<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head class="fly-header layui-bg-black" th:include="admin/header.html :: head"></head>
</header>
<body>
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-comm" data-type="batchdel">批量删除</button>
      </div>
      <table id="LAY-app-content-comm" lay-filter="LAY-app-content-comm" class="demo"></table>
      <script type="text/html" id="table-post-type">
        {{#  if(d.postType ==1 || d.postType ==0){ }}
        <div>表白</div>
        {{#  } }}
        {{#  if(d.postType ==2 ){ }}
        <div>分享</div>
        {{#  } }}
        {{#  if(d.postType ==3 ){ }}
        <div>吐槽</div>
        {{#  } }}
      </script>
      <script type="text/html" id="layuiadmin-app-cont-tagsbar1">
        <input type="checkbox"  name="open" value="{{d.postId}}" lay-skin="switch" lay-filter="Status" lay-text="已发布|待审核" {{ d.postState == 1 ? 'checked' : '' }}>        </script>
      <script type="text/html" id="table-content-com-update">
        <!--          <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>-->
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
      </script>
    </div>
  </div>
</div>
<script th:src="@{/layuiadmin/layui/layui.js}"></script>
<script>
  layui.config({
    base: '/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'contlist', 'table'], function(){
    var $ = layui.$
            ,form = layui.form
            ,table = layui.table;


    //监听搜索
    form.on('submit(LAY-app-contcomm-search)', function(data){
      var field = data.field;
    });

    table.render({
      elem: '.demo'
      ,url: '/postList',
      page: true //开启分页
      ,title: '用户数据表'
      ,cols: [
        [
          {type: 'checkbox',width:40},
          {field:'postId', title:'帖子ID', width:80,align:'center',unresize:true},
          {field:'postType', title:'帖子类型', width: 100,align:'center',unresize:true,toolbar:'#table-post-type'},
          {field:'postContent', title:'帖子内容', width: 310,unresize:true},
          {field:'postVisitedCount', title:'帖子访问次数', width: 120,align:'center',unresize:true},
          {field:'postLikedCount', title:'帖子点赞次数', width: 120,align:'center',unresize:true},
          {field:'postCreateTime', title:'发布时间', width:180,align:'center',unresize:true},
          {field:'right', title:'发布状态',toolbar:'#layuiadmin-app-cont-tagsbar1',unresize:true, width:150,align:'center',unresize: true},
          {field:'right', title:'操作',toolbar:'#table-content-com-update',unresize:true, align:'center',width: 145},
        ]
      ]
      ,limits: [5,10,20,50,100,200,500]  //每页条数的选择项，默认：[10,20,30,40,50,60,70,80,90]。
      ,limit: 10 //每页默认显示的数量
    });

    form.on('switch(Status)', function(obj){
      var postId = this.value;
      var postStatus = obj.elem.checked?1:2;
      //请求接口
      $.post('/postStatus',{
        "postId":postId,
        "postStatus":postStatus
      },'json');
    });
    //点击事件
    var active = {
      batchdel: function(){
        var checkStatus = table.checkStatus('LAY-app-content-comm')
                ,checkData = checkStatus.data; //得到选中的数据

        if(checkData.length === 0){
          return layer.msg('请选择数据');
        }

        layer.confirm('确定删除吗？', function(index) {
          $.ajax({
            url:'/postDeletes',
            data:{"delete":JSON.stringify(checkData)},
            dataType:'json',
            method:'post',
            success:function (res) {
              if (res.success){
                layer.msg('删除成功', {
                  offset: '15px'
                  ,icon: 1
                  ,time: 1000
                });
              }else {
                layer.msg('删除失败', {
                  offset: '15px'
                  ,icon: 1
                  ,time: 1000
                });
              }
              table.reload('LAY-app-content-comm');
            }
            ,error: function(){
              layer.alert('系统错误，请联系管理员！', {
                icon: 2,
                skin: 'layer-ext-moon' //该皮肤由layer.seaning.com友情扩展。关于皮肤的扩展规则，去这里查阅
              });
            }
          });

          table.reload('LAY-app-content-comm');
        });
      }
    }

    //监听行工具栏
    table.on('tool(LAY-app-content-comm)',function (obj) {
      //获取行数据
      var data = obj.data;
      //点击删除
      if (obj.event === 'del'){
        layer.confirm('确定删除吗？', function(index){
          $.ajax({
            url:'/postDelete',
            data:{"postId":data.postId},
            dataType:'json',
            method:'post',
            success:function (res) {
              if (res.success){
                layer.msg('删除成功', {
                  offset: '15px'
                  ,icon: 1
                  ,time: 1000
                });
              }else {
                layer.msg('删除失败', {
                  offset: '15px'
                  ,icon: 1
                  ,time: 1000
                });
              }
              table.reload('LAY-app-content-comm');
            }
            ,error: function(){
              layer.alert('系统错误，请联系管理员！', {
                icon: 2,
                skin: 'layer-ext-moon' //该皮肤由layer.seaning.com友情扩展。关于皮肤的扩展规则，去这里查阅
              });
            }
          });
          layer.close(index);
        });
      }
    })

    $('.layui-btn.layuiadmin-btn-comm').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
  });
</script>
</body>
</html>
